#
# Wrapper class for CGI class
# Difference: Returns all output from param() as UTF-8 tagged strings
#
# Note - there are warn()s on all the code paths, but the ones that have
# been tested are commented out. Let's see if the other ones get called.
#

package Counter::CGI;

use strict;

use vars qw(@ISA);

@ISA = qw(CGI);

use CGI;
use Encode;

sub param {
    my $self = shift;
    if (!defined($_[0])) {
	#warn "Counter::CGI returning std on no args\n";
	return $self->SUPER::param(@_);
    } elsif (@_ > 1) {
	warn "Counter::CGI returning std on multi args\n";
	return $self->SUPER::param(@_);
    }
    # param() is also called internally by the CGI library
    if ($_[0] =~ /^\./) {
	#warn "Counter::CGI returning std on internal call\n";
	return $self->SUPER::param(@_);
    }
    if (wantarray) {
	#warn "Counter::CGI param(", $_[0], ") return array\n";
	my @ret = $self->SUPER::param(@_);
	for my $ix (0..$#ret) {
	    $ret[$ix] = _convtoutf8($ret[$ix]);
	}
	return @ret;
    } else {
	#warn "Counter::CGI param(", $_[0], ") return single\n";
	my $ret = $self->SUPER::param(@_);
	return _convtoutf8($ret);
    }
}

sub _convtoutf8 {
    my $str = shift;
    if (utf8::is_utf8($str)) {
	warn "Counter::CGI - already converted\n";
	return $str;
    } else {
	return Encode::decode("utf-8", $str);
    }
}



1;
